<?php

require_once 'Zend/Controller/Action.php';

abstract class Base_Controller_Action extends Zend_Controller_Action
{
    protected $form = null;
    
    protected $enableCache = true;
    protected $isTranslate = true;

    public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array())
    {
		Zend_Controller_Action_HelperBroker::addPrefix('Base_Controller_Action_Helper_');
        parent::__construct($request, $response, $invokeArgs);
        
        if ($this->isTranslate === true) {
            Zend_Registry::set('Zend_Cache', Zend_Cache::factory('Page', 'File',
                array('lifeTime'  => 5, 'automatic_serialization' => true),
                array('cache_dir' => MODULES_PATH .'/../private/cache/')
            ));
            
            Zend_Translate::setCache(Zend_Registry::get('Zend_Cache'));
            Zend_Registry::set('Zend_Translate', new Zend_Translate(
                array(
                    'adapter' => 'gettext',
                    'locale'  => 'zh_CN',
                    'content' => MODULES_PATH .'/'. $request->getModuleName() .'/languages/zh_CN.mo',
                )
            ));
        }
		
        Base_Loader_Autoload::factory($request->module);
		$this->view->addHelperPath("Base/View/Helper", "Base_View_Helper");
    }
    
    public function preDispatch()
    {
        $access = $this->_helper->CheckPermission($this->_request);
        if (!$access) {
            switch ($access) {
                case 0 :
                    return $this->_helper->ErrorPage(403);
                default :
                    return $this->_helper->ErrorPage(403);
            }
        }
    }
    
    protected function create($model)
    {
        //check model
        if (is_string($model)) {
            $modelName = 'Model_'. ucfirst($model);
        }
        
        //check form
        if ($this->form === null) {
            throw new Zend_Exception('form is not exist');
        }
        
        $this->view->form = $this->form;
        
        //check post
        if ($this->_request->isPost()) {
            if ($this->form->isValid($this->_request->getPost())) {
                if (!is_object($model)) {
                    $model = new $modelName();
                }
                
                //save post
                try {
                    $model->makeByForm($this->form);
                    $model->save();
                }
                catch (Base_Db_Exception $e) {
		            return $e->setFormError($this->form);
                }
                
                return $this->_forward('success','post','mnt');
            }
        }
    }
    
    protected function modify($model)
    {
        //check form
        if ($this->form === null) {
            throw new Zend_Exception('form is not exist');
        }
        
        //check model
        if (is_string($model)) {
            $modelName = 'Model_'. ucfirst($model);
            
            $model = new $modelName();
            try {
                $model->read($this->_getParam(
                    $model->getDbTable()->getPrimary()
                    ));
            } catch (Base_Model_Exception $e) {
                $this->_forward('notfound','post','default');
            }
        }
        
        $this->view->form = $this->form;
        
        //check post
        if ($this->_request->isPost()) {
            if ($this->form->isValid($this->_request->getPost())) {

                //save post
                try {
                    $model->makeByForm($this->form);
                    $model->save();
                }
                catch (Base_Db_Exception $e) {
		            return $e->setFormError($this->form);
                }
                
                return $this->_forward('success','post','mnt');
            }
        }
        else {
            //set default
            $this->form->setDefaults($model->toArray());
        }
    }
    
    protected function delete($model)
    {
        //check form
        if ($this->form === null) {
            throw new Zend_Exception('form is not exist');
        }
        
        //check model
        if (is_string($model)) {
            $modelName = 'Model_'. ucfirst($model);
            
            $model = new $modelName();
            try {
                $model->read($this->_getParam(
                    $model->getDbTable()->getPrimary()
                    ));
            } catch (Base_Model_Exception $e) {
                $this->_forward('notfound','post','mnt');
            }
        }
        
        $this->view->form = $this->form;
        
        //check post
        if ($this->_request->isPost()) {
            if ($this->form->isValid($this->_request->getPost())) {
                //drop post
                try {
                    $model->drop();
                } catch (Base_Db_Exception $e) {
		            return $e->setFormError($this->form);
                }
                
                return $this->_forward('success','post','mnt');
            }
        }
        
        //set form
        $this->form->setDefaults($model->toArray());
    }
}